/**
 * @Description:各个模块通用工具类
 * @updateTime shusm 2017/11/3 19:09
 */
var Utils = {};
Utils.VERSION = '0.1';


/*
 　 *　方法:Array.baoremove(dx)
 　 *　功能:删除数组元素.
 　 *　参数:dx删除元素的下标.
 　 *　返回:在原数组上修改数组.
 　 */
//也可以用splice来实现.
Array.prototype.baoremove = function(dx) {
    if(isNaN(dx)||dx>this.length){return false;}
    this.splice(dx,1);
};

/**
 * http://localhost:8080/xxx/default#/xxx.html?type=12 URL中解析参数
 * 
 * @returns {{}}
 */
Utils.urlArgs = function() {
	var args = {};
	// var query = location.hash.substr(location.hash.lastIndexOf('?')+1)
	var query = location.href.substr(location.href.lastIndexOf('?') + 1);
	var pairs = query.split("&");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1)
			continue;
		var name = pairs[i].substring(0, pos);
		var value = pairs[i].substring(pos + 1);
		value = decodeURIComponent(value);
		args[name] = value;
	}
	return args;
};

/**
 * http://localhost:8080/xxx/default#/xxx.html?type=12 dialog弹窗中的URL中解析参数
 * 
 * @returns {{}}
 */
Utils.dialogArgs = function() {
	var args = {};
	var query = location.search.substr(location.search.lastIndexOf('?') + 1)
	var pairs = query.split("&");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1)
			continue;
		var name = pairs[i].substring(0, pos);
		var value = pairs[i].substring(pos + 1);
		value = decodeURIComponent(value);
		args[name] = value;
	}
	return args;
};

Utils.initLayui = function () {
    //注意：折叠面板 依赖 element 模块，否则无法进行功能性操作
    layui.use('element', function(){
        var element = layui.element;
    });
}

// ******************字典formatter start****************************//

Utils.commonFmt = function (value,row,index,array,fmtName) {
    var retStr = '';
    array.forEach(function(el,index,array){
        if (el.value == value){
            retStr = el.name;
        }
    });
    if(retStr == ''){
        console.info("FormatterJS." + fmtName + " 使用 有误！");
    }
    return retStr;
};

// ******************字典formatter End******************************//

//根据url下载模板
Utils.download =function (downloadUrl,fileName) {
    window.open("../../common/file/download?"
        + "url="+ downloadUrl
        + "&fileName="+fileName
        + '&token=' + localStorage.getItem("token"),'_self');
};

Utils.uploadDlg =function (classRoomId) {
    dialogOpen({
        id : 'uploadDlg',
        title: '上传PPT',
        url: 'srblive_html/file/upload.html?_' + $.now(),
        width: '40%',
        height: '45%',
        // btnSize: 1,
        btnAlign: 'c',
        btn: [],
        success: function (iframeId) {
            top.frames[iframeId].vm.classRoomId = classRoomId;
        },
        yes: function (iframeId) {
            top.frames[iframeId].vm.acceptClick('openFileGridList');
        }
    });
};

//打开文件列表页面
Utils.openFileGridList =function (classRoomId) {
    dialogOpen({
        id : 'openFileGridList',
        title: '上传PPT',
        url: 'srblive_html/file/listForDlg.html?classRoomId='+classRoomId+'&_' + $.now(),
        width: '70%',
        height: '70%',
        // btnSize: 1,
        btnAlign: 'c',
        btn: [],
        success: function (iframeId) {
            top.frames[iframeId].vm.keyword.classRoomId = classRoomId;
        },
        yes: function (iframeId) {
            // top.frames[iframeId].vm.acceptClick();
        }
    });
};

/**************************二级联动********************************************/
Utils.getLiveRoomTypes = function (options) {
    var defaults = {
        pid: null,
        serviceType:'getParent',
        success : function () { },
        /*------------------------------------------*/
        url: "../../srblive/liveroomparenttype/getPtypes",
        id : 'layerForm',
        type : "post",
        dataType : "json",
    };
    var options = $.extend(defaults,options);
    if(options.serviceType && options.serviceType=='getSon'){
        options.url = '../../srblive/liveroomtype/getSonTypes';
    }
    $.ajax({
        url: options.url,
        data: {pid: options.pid},
        type: options.type,
        dataType: options.dataType,
        success: function (result) {
            if (result.length == 0) {
                return false;
            }
            options.success(result);
        }
    });
};
/**************************二级联动********************************************/

Utils.reloadGrid = function(gridId,opts){
    if(opts==null){
        $('#'+gridId).bootstrapTable('refresh');
    }else{
        $('#'+gridId).bootstrapTable('refresh',opts);
    }
}